package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.constant.MessageConstant;
import com.msun.dao.CheckGroupDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.CheckGroup;
import com.msun.pojo.CheckItem;
import com.msun.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */

@Service(interfaceClass = CheckGroupService.class)
//@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(Map<String,Object> map) {
        Object checkGroupOBJ =  map.get("checkGroup");
        Object checkItemOBJ =map.get("checkItem");

        String checkGroupSTR = JSONObject.toJSONString(checkGroupOBJ);
        CheckGroup checkGroup = JSONObject.parseObject(checkGroupSTR, CheckGroup.class);
        String checkItemSTR = JSONObject.toJSONString(checkItemOBJ);
        List<Integer> checkItem = JSONObject.parseObject(checkItemSTR, List.class);
        // 新增后把id拿回来
        checkGroupDao.addGroup(checkGroup);
        for (Integer itemId : checkItem) {
            checkGroupDao.addRefer(checkGroup.getId(),itemId);
        }

    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据ID查询检查组
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组ID查询关联的检查项ID
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit (Map<String,Object> map) {
        Object checkGroupOBJ = map.get("checkGroup");
        Object checkItemOBJ =  map.get("checkItem");

        String checkGroupSTR = JSONObject.toJSONString(checkGroupOBJ);
        CheckGroup checkGroup = JSONObject.parseObject(checkGroupSTR, CheckGroup.class);
        String checkItemSTR = JSONObject.toJSONString(checkItemOBJ);
        List<Integer> checkItem = JSONObject.parseObject(checkItemSTR, List.class);


        checkGroupDao.edit(checkGroup);
        // todo 保存前先把数据删除
        Integer groupId = checkGroup.getId();
        checkGroupDao.deleteRefer(groupId);
        // 重新添加关系
        for (Integer itemId : checkItem) {
            checkGroupDao.addRefer(groupId,itemId);
        }


    }

    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findPage(queryString);
        for (CheckGroup checkGroup : page) {
            List<CheckItem> itemIdByGroup = checkGroupDao.findItemIdByGroup(checkGroup.getId());

            checkGroup.setCheckItems(itemIdByGroup);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 通过套餐id查询检查组名称
     */
    @Override
    public List<String> fingBySetmaelId(Integer setId) {
        return checkGroupDao.fingBySetmaelId( setId);
    }

    @Override
    public void delete(Integer id) {
//        int i1 =checkGroupDao.group4item();
        int i = checkGroupDao.group4setmeal(id);
        if (i!=0){
            throw new RuntimeException(MessageConstant.DELETE_CHECKGROUP_FAIL1);

        }
        // 先删组和套餐项的引用
        checkGroupDao.deleteRefer(id);
        // 再删主体
        checkGroupDao.delete(id);

    }
    //建立检查组和检查项多对多关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
    @Override
    public List<CheckGroup> findAllCheckGroup(Integer checkGroupId) {
        List<CheckGroup> checkGroups = checkGroupDao.findCheckGroups(checkGroupId);

        return checkGroups;
    }
}
